package com.smartrobot.smartrobot.activity.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.smartrobot.smartrobot.R;

/**
 * Created by Administrator on 2014/12/22.
 */
public class testWebView extends Activity{

    private ProgressBar progressBar;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.page_product);

        init();

    }

    void init(){
        initView();
    }

    void initView(){

        // 设置文件存储位置
       String  path = this.getFilesDir().getAbsolutePath() + "cache";

        progressBar = (ProgressBar)findViewById(R.id.webview_pb);
        webView = (WebView)findViewById(R.id.webview);


        webView.setWebViewClient(new webClient());
        webView.setWebChromeClient(new chromeClient());


        // 设置webview特性
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 使用缓冲系统

        // 设置自动建立缓存文件
        webView.getSettings().setDomStorageEnabled(true);// 需要手动删除缓存
        webView.getSettings().setDatabaseEnabled(true);// 使用数据库
        // 设置缓存目录 将数据库文件盒缓存文件存储
        webView.getSettings().setDatabasePath(path);
        webView.getSettings().setAppCachePath(path);
        // 设置自动cache功能
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);
        // 设置最大缓存
        webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);

        //可以放大缩小网页
        webView.setInitialScale(30);
        webView.getSettings().setSupportZoom(true);
//        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
//        webView.getSettings().setDisplayZoomControls(true);

        // 设置开始时加载的主页
        webView.loadUrl("http://www.baidu.com/");

    }


    // 监听：页面加载情况
    class chromeClient extends WebChromeClient {
        public void onProgressChanged(WebView view, int newProgress) {

            progressBar.setVisibility(ProgressBar.VISIBLE);
            progressBar.setProgress(newProgress);

            if (newProgress == 100) {
                progressBar.setVisibility(ProgressBar.GONE);
            }
        }

        // 当达到最大缓存时，扩充缓存的小 扩大两倍
        public void onReachedMaxAppCacheSize(long requiredStorage, long quota,
                                             WebStorage.QuotaUpdater quotaUpdater) {

            quotaUpdater.updateQuota(requiredStorage * 2);

        }
    }


    // 重写WebViewClient 否则无法使用本程序打开网页
    class webClient extends WebViewClient {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            // 点击后退时不会进行这一步操作 同时当前网页不会重新加载 除非刷新
            try{
                view.loadUrl(url);
            }catch(Exception e){
                e.printStackTrace();
            }

            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {


            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            super.onPageFinished(view, url);
        }

    }



}
